/**
 * Created by Administrator on 2017/4/5.
 */

var searchCar=new Vue({
    el:'#searchCar',
    data:{
        carlist:{},
        title:'',
        type_id:{},
        href:{},
        page:{},
        record:null,
        pageL:5,
        //search_box
        cartype:{},
        picked:'全部'
    },
    methods:{
        getData:function(){
//          page初始化
            this.href=window.location.href.split("&page=")[0].split("?type_id=")[0];
            if(window.location.href.search("page")===-1){
                this.page=1;
            }else{
                this.page=parseInt(window.location.href.split("&page=")[1]);
            }
//          type_id初始化
            var id=window.location.href.split("&page=")[0].split("?type_id=")[1];
//bus中巴大巴;婚庆;丰田考斯特[18,20];商务会议;别克、帕萨特、凯美瑞、奔驰唯雅诺
            if(id&&isNaN(parseInt(id))){
                switch (id){
                    case "navTravel":
                        this.title="旅游租车";
                        this.type_id="navBus";
                        break;
                    case "navAir":
                        this.title="机场接送";
                        this.type_id="navAir";
                        break;
                    case "navBus":
                        this.title="企业班车";
                        this.type_id="navBus";
                        break;
                    case "navMarry":
                        this.title="婚庆租车";
                        this.type_id="navMarry";
                        break;
                    case "navCoaster":
                        this.title="丰田考斯特";
                        this.type_id="navCoaster";
                        break;
                    case "navCar":
                        this.title="商务会议";
                        this.type_id="navCar";
                        break;
                    case "all":
                        this.title="搜索结果";
                        this.type_id="all";
                        break;
                }
            }else if(id===undefined){
                this.title="搜索结果";
                this.type_id="all";
            }else if(!isNaN(parseInt(id))){
                this.title="搜索结果";
                this.type_id=parseInt(id);
            }else{console.log("error_type_id");}
//                ajax
            var _self=this;
            $.ajax({
                type: "GET",
                url: "../data/search.php",
                data:{
                    "type_id":_self.type_id,
                    "page":_self.page,
                    "pageL":5
                },
                success: function (data, textStatus, jqXHR) {
                    var arr=data;
//                        总记录数
                    _self.record=data.record;
                    delete arr.record;
//                        data数据处理
                    for(var each in arr){each++;}
                    var len=each;
                    for(var i= 0,str="";i<len;i++){
                        if(arr[i].car_num!==undefined){
                            break;
                        }
                        else if(arr[i].pro_content===undefined){
                            str='<strong>可供应数量：</strong>10';
                        }
//                            arr[0].pro_content
                        else if((arr[i].pro_content.search("车牌号")!==-1)||(arr[i].pro_content.search("可供应数量")!==-1)){
                            str=arr[i].pro_content
                                .split('<p style="white-space: normal;">')[1]
                                .split('</p>')[0];
                        }
                        arr[i]["car_num"]=str;
                    }
                    _self.carlist=arr;
//                        page
//                获得href.page;若prev-1<=0,prev不可用，else prev=page-1;
//                sql获取记录数num,num/5=pageMax,若next+1>pageMax,next不可用，else next=page+1
                    if(_self.page>1){
                        $(".prev").attr("href",_self.href+"?type_id="+_self.type_id+"&page="+(_self.page-1));
                        $(".prev").removeClass("disabled");
                    }else{
                        $(".prev").attr("href","javascript:void(0);");
                        $(".prev").addClass("disabled");
                    }
                    if(_self.page*_self.pageL<_self.record){
                        $(".next").attr("href",_self.href+"?type_id="+_self.type_id+"&page="+(_self.page+1));
                        $(".next").removeClass("disabled");
                    }
                    else{
                        $(".next").attr("href","javascript:void(0);");
                        $(".next").addClass("disabled");
                    }

                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log(XMLHttpRequest, textStatus, errorThrown);
                }
            });
        },
        //search_box
        getData_:function(){
            var href=window.location.href.split("com")[0]+"com";
            var _self=this;
            $.ajax({
                type:"GET",
                //绝对定位
                url:href+"/m/data/cartype.php",
                success:function(data, textStatus, jqXHR){
                    var obj={"tupe_id":"all","type_name":"全部"};
                    _self.cartype=data;
                    _self.cartype.unshift(obj);
                },
                error:function(XMLHttpRequest, textStatus, errorThrown){
                    console.log(XMLHttpRequest, textStatus, errorThrown);
                }
            })
        },
        searchHref:function(e){
            var elem=e.target;
            if(elem.nodeName=="LABEL"){
                console.log(111);
                elem=elem.firstElementChild;
                type_id=elem.getAttribute("type_id");
                console.log(elem,type_id);

            }else if(elem.nodeName=="input"){
                console.log(222);
                type_id=elem.getAttribute("type_id");
                console.log(type_id);
            }
            window.location.href="http://www.ashlybus.com/m/other/search.html?type_id="+type_id;
            //var type_name=$.trim($(".select_text").html());
            //var type_id="all";
            //if(!isNaN(parseInt(type_name))){
            //    type_id=$(":radio:checked").attr("type_id");
            //}
            ////绝对定位
            //window.location.href="http://www.ashlybus.com/m/other/search.html?type_id="+type_id;
        },
        selectClick:function(){
            var $popup_box=$(".popup_box");
            $popup_box.fadeIn();
        },
        colseClick:function(){
            $(".popup_box").fadeOut();
        },
        selected:function(){
            $(":radio:checked").parent().addClass("inputS")
                .siblings().removeClass("inputS");
            this.searchHref();
        }
    }
});
$(document).ready(function(){
    searchCar.getData();
    searchCar.getData_();
    $(".popup_box").click(function(e){
        if(e.target.className=="popup_box"){
            if(e.target.className=="box"){
                return 0;
            }else{
                $(".popup_box").fadeOut();
            }
        }
    })
});

